package com.ellecity06.debugtools.widgets.dialog

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.Gravity
import android.view.LayoutInflater
import android.view.WindowManager
import com.ellecity06.debugtools.R
import com.ellecity06.debugtools.databinding.DebugCustomAlertDialogBinding

/**
 * @author ellecity06
 * @time 2023/4/20 17:24
 * @des
 */
internal class CustomAlertDialog(context: Context) : Dialog(context, R.style.custom_dialog) {
    val binding: DebugCustomAlertDialogBinding by lazy { DebugCustomAlertDialogBinding.inflate(LayoutInflater.from(context)) }
    private val mDisplayMetrics: DisplayMetrics by lazy { context.resources.displayMetrics }
    val screenWidth: Int by lazy { mDisplayMetrics.widthPixels }
    val screenHeight: Int by lazy { mDisplayMetrics.heightPixels }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        initAttributes((screenWidth * 0.8).toInt())
        binding.tvCancel.setOnClickListener { dismiss() }

    }

    fun show(content: String, confirm: (() -> Unit)? = null) {
        binding.tvContent.text = content
        binding.tvConfirm.setOnClickListener {
            dismiss()
            confirm?.invoke()
        }
        super.show()
    }

    /**
     * 初始化Dialog
     * @param width 设置宽度
     * @param height 设置高度
     * @param gravity 设置显示位置
     * @param isCancelable 是否禁用back键
     * @param isTouchOutsideDismiss 点击阴影是否消失
     * @param animation 设置动画资源文件 大于0则表示开启
     * @param dimAmount 阴影浓度
     */
    private fun initAttributes(
        width: Int = WindowManager.LayoutParams.MATCH_PARENT,
        height: Int = WindowManager.LayoutParams.WRAP_CONTENT,
        gravity: Int = Gravity.CENTER,
        isTouchOutsideDismiss: Boolean = true,
        isCancelable: Boolean = true,
        animation: Int = -1,
        dimAmount: Float = 0.5f
    ) {
        val layoutParams = window!!.attributes
        when (width) {
            WindowManager.LayoutParams.MATCH_PARENT -> layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT
            WindowManager.LayoutParams.WRAP_CONTENT -> layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT
            else -> layoutParams.width = width
        }
        when (height) {
            WindowManager.LayoutParams.MATCH_PARENT -> layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT
            WindowManager.LayoutParams.WRAP_CONTENT -> layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT
            else -> layoutParams.height = width
        }
        //设置显示位置
        layoutParams.gravity = gravity
        //设置是否屏蔽返回键与点击空白区域不关闭Dialog
        setCancelable(isCancelable)
        setCanceledOnTouchOutside(isTouchOutsideDismiss)
        //设置是否开启动画
        if (animation > 0) {
            //如果动画资源为空,则设置成默认动画
            if (animation != 0) {
                layoutParams.windowAnimations = animation
            } else {
                layoutParams.windowAnimations = R.style.Animation_Design_BottomSheetDialog
            }
        }
        layoutParams.dimAmount = dimAmount
        //设置属性
        window!!.attributes = layoutParams
    }
}

internal fun Context.showAlert(content: String, confirm: (() -> Unit)? = null) {
    CustomAlertDialog(this).show(content = content, confirm = confirm)
}